package com.share.rest;

import com.share.component.MsgSender;
import com.share.entity.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.AsyncRabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @desc:
 * @author:caifan
 * @date:2020/3/31
 */
@RestController
@RequestMapping("/rest/producer")
@Slf4j
public class RabbitProducer {

    /*@Autowired
    RabbitAdmin rabbitAdmin;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    AsyncRabbitTemplate asyncRabbitTemplate;*/

    @Autowired
    MsgSender msgSender;

    @GetMapping("/deadTest")
    public String deadTest(@RequestParam String name) {
        msgSender.sendDeadTest(name);
        return "hello:" + name;
    }

   /* @PostMapping(value = "/msg1", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sendMessage(@RequestBody Map<String, String> map) {
        DirectExchange directExchange = new DirectExchange("myDirectTest", true, false);
        rabbitAdmin.declareExchange(directExchange);
        Queue queue = new Queue("myQueue", true, false, false);
        rabbitAdmin.declareQueue(queue);
        Binding binding = BindingBuilder.bind(queue).to(directExchange).with("abc.ade");
        rabbitAdmin.declareBinding(binding);

        rabbitTemplate.convertAndSend("myDirectTest", "abc.ade", map);
        return "OK";
    }*/

    @PostMapping("/msg01")
    public void sendMsg1(@RequestBody Object object) {
//        MessageProperties messageProperties = new MessageProperties();
        msgSender.sendMsg(object, new HashMap<>());
    }

    @PostMapping("/msg02")
    public void sendMsg2(@RequestBody Order order) throws Exception {
        msgSender.sendOrderMsg(order);
    }

    @PostMapping("/msg03")
    public void sendMsg3(@RequestBody Order order) {
        log.info("send msg time:{}", new Date());
        msgSender.sendDelayMessage(order);
    }

    @PostMapping("/msg04")
    public void sendMsg04(@RequestBody Object object) {
        msgSender.sendMsg2(object);
    }

    /*@PostMapping("/fanout")
    public void sendFanout(@RequestBody Order order) {
        rabbitTemplate.convertAndSend("share.boot.fanoutExchange","", order);
    }*/

    /*@PostMapping("/asyncMsg")
    public void sendAsyncMsg(@RequestBody Object obj) {
        rabbitTemplate.convertAndSend("springboot.direct.exchange", "springboot.key4", "hello");
        rabbitTemplate.convertAndSend("springboot.direct.exchange", "springboot.key4", obj);
        asyncRabbitTemplate.convertSendAndReceive("share.boot.topicExchange", "springboot.topic", obj);
        asyncRabbitTemplate.convertSendAndReceive("springboot.direct.exchange", "springboot.key4", obj);
    }*/
}
